package com.xique.door.controller;


import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.bean.domain.PermissionGroupDevice;
import com.xique.door.bean.domain.PermissionGroupUser;
import com.xique.door.bean.request.commandV1.DeviceCommandGetRecStatusRequest;
import com.xique.door.bean.request.permissionGroup.PermissionGroupDeleteUserRequest;
import com.xique.door.bean.request.permissionGroup.PermissionGroupSelectUserRequest;
import com.xique.door.bean.request.permissionGroup.PermissionGroupUserRequest;
import com.xique.door.biz.service.IPermissionGroupUserBizService;
import com.xique.door.biz.service.IDeviceCommandService;
import com.xique.door.constant.AccessCommandConstant;
import com.xique.door.service.IPermissionGroupDeviceService;
import com.xique.door.service.IPermissionGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Deprecated
@Controller
@RequestMapping("/oms/permissionGroupUser")
public class PermissionGroupUserController extends BaseController {

    private String prefix = "oms/permissionGroupUser";

    @Autowired
    private IPermissionGroupService permissionGroupService;

    @Autowired
    private IDeviceCommandService deviceCommandService;

    @Autowired
    private IPermissionGroupUserBizService permissionGroupUserBizService;

    @Autowired
    private IPermissionGroupDeviceService permissionGroupDeviceService;

    /**
     * 分配用户
     */
     @PreAuthorize(hasPermi="oms:permissionGroup:edit")
    @GetMapping("/addUser/{id}")
    public String addUser(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("group", permissionGroupService.selectPermissionGroupById(id));
        return prefix + "/addUser";
    }

    /**
     * 查询已分配用户权限组列表
     */
     @PreAuthorize(hasPermi="oms:permissionGroup:list")
    @PostMapping("/addUser/allocatedUserList/{parkId}")
    @ResponseBody
    public TableDataInfo allocatedUserList(PermissionGroupUserRequest request, ModelMap mmap)
    {
        startPage();
        List<PermissionGroupUser> list = permissionGroupUserBizService.selectAllocatedUserList(request);
        //TODO 用户同步状态
        DeviceCommandGetRecStatusRequest recStatusRequest = new DeviceCommandGetRecStatusRequest();
        recStatusRequest.setUsers(list);
        recStatusRequest.setGroupId(request.getGroupId());
        recStatusRequest.setClassName("User");

        return getDataTable(deviceCommandService.getRecStatus(recStatusRequest));
    }

    /**
     * 查询未分配用户权限组列表
     */
     @PreAuthorize(hasPermi="oms:permissionGroup:list")
    @PostMapping("/addUser/unallocatedUserList/{parkId}")
    @ResponseBody
    public TableDataInfo unallocatedUserList(PermissionGroupUserRequest request, ModelMap mmap)
    {
        List<PermissionGroupDevice> permissionGroupDeviceList = permissionGroupDeviceService.selectPermissionGroupDeviceListByGroupIds(Collections.singletonList(request.getGroupId()));
        long count = permissionGroupDeviceList.stream().filter(item -> AccessCommandConstant.DeviceType.HK.equals(item.getDevModel())).count();
        if (count > 0) {
            request.setHkRegistFlag(1L);
        }
        startPage();
        request.setStatus(1L);
        List<PermissionGroupUser> list = permissionGroupUserBizService.selectUnallocatedUserList(request);

        TableDataInfo tableDataInfo = getDataTable(list);

        List<PermissionGroupUser> lists = new ArrayList<>(list.size());
        list.forEach(item -> {
            item.setGroupId(request.getGroupId());
            lists.add(item);
        });

        tableDataInfo.setRows(lists);

        return tableDataInfo;
    }

    /**
     * 新增权限组用户
     */
    @GetMapping("/addUser/selectUser/{id}")
    public String selectUser(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("group", permissionGroupService.selectPermissionGroupById(id));
        return prefix + "/selectUser";
    }

    /**
     * 批量选择用户授权
     */
    @Log(title = "权限管理", businessType = BusinessType.GRANT)
    @PostMapping("/addUser/selectUserAll")
    @ResponseBody
    public AjaxResult selectUserAll(PermissionGroupSelectUserRequest request)
    {
        return permissionGroupUserBizService.addUsers(request);
//        return permissionGroupService.selectUsers(request);
    }

    /**
     * 取消授权
     */
    @Log(title = "权限管理", businessType = BusinessType.GRANT)
    @PostMapping("/addUser/cancelUser")
    @ResponseBody
    public AjaxResult cancelUser(PermissionGroupDeleteUserRequest request)
    {
        return permissionGroupUserBizService.deleteUser(request);
    }

    /**
     * 批量取消授权
     */
    @Log(title = "权限管理", businessType = BusinessType.GRANT)
    @PostMapping("/addUser/cancelUserAll")
    @ResponseBody
    public AjaxResult cancelUserAll(PermissionGroupDeleteUserRequest request)
    {
        return permissionGroupUserBizService.deleteUsers(request);
    }
}
